<?php

namespace App\Admin\Renderable;

use App\Admin\Repositories\Jobposition;
use App\Models\CategoryModel;
use App\Models\CompanyModel;
use App\Models\JobpositionModel;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;
use SuperEggs\DcatDistpicker\DcatDistpickerHelper;

class JobPositionTab extends LazyRenderable
{

    public function grid(): Grid
    {
        return Grid::make(new Jobposition(['company', 'categories']), function (Grid $grid) {
            $notIds = $this->payload['notIds']??[];
            if ($notIds){
                $grid->model()->whereNotIn('id', $notIds);
            }
            $grid->column('id');
            $grid->column('position_name','岗位');
            $grid->column('company.company_name', '公司');
            $grid->column('region')->display(function () {
                if (!$this->region_region) {
                    return '全国';
                }
                return DcatDistpickerHelper::getAreaName($this->region_province) . DcatDistpickerHelper::getAreaName($this->region_city) . DcatDistpickerHelper::getAreaName($this->region_region);
            });

            $grid->paginate(50);
            $grid->disableActions();

            $grid->filter(function (Grid\Filter $filter) {
                $companyList = CompanyModel::query()->pluck('company_name', 'id')->toArray();
                $filter->expand();
                $filter->panel();
                $filter->equal('id')->width(3);
                $filter->like('position_name','职位名称')->width(4);
                $filter->equal('company.id', '公司')->select($companyList)->width(4);
            });
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $category = CategoryModel::query()->where('cur_status', 1)->pluck('name', 'id')->toArray() ?? [];
                $selector->select('categories.id', '分类', $category);
                $selector->selectOne('cur_sss', '状态', JobpositionModel::STATUS_LIST, function ($query, $value) {
                    $query->where('cur_status', $value);
                });
            });
            // 禁用过滤器按钮
            $grid->disableFilterButton();
        });
    }
}